/**
 * Created by LiuJie on 2017/10/16.
 */
;(function ($,window,undefined) {
    var validator = new Validator();
    var _validSpanElem = '<span class="validSpan">' +
        '<span></span>' +
        '<i class="fa fa-sort-desc validfa"></i>' +
        '</span>';


    /**
     *
     * @param params:[
     *      {
     *          name:"",//input的name
     *          validate:[
     *              {
     *                  type:"",//必须
     *                  value:"",//可选
     *                  message:""//必须
     *              }
     *          ] //可以为对象
     *      }
     * ]
     */
    $.fn.validForm = function (params) {
        var $this = this instanceof jQuery ? this : $(this);
        $this.off("blur").on("blur","input",function () {
            if($.isArray(params) && params.length){
                for(var i = 0,
                        _len = params.length;i<_len;i++){
                    var _param = params[i];
                    var _name = _param["name"],
                        _validates = _param["validate"];
                    if($(this).attr("name") == _name){
                        var $elem = $this.find("[name='"+_name+"']");
                        var _value = "";
                        if($elem[0].nodeName === "INPUT" || $elem[0].nodeName=="SELECT"){
                            _value = $elem.val();
                        }else{
                            _value = $elem.text();
                        }
                        var result = true;
                        if($.isArray(_validates) && _validates.length){
                            for(var j=0,_vLen=_validates.length;j<_vLen;j++){
                                result =  $.validForm.validate($elem,_value,_validates[j]);
                                if(!result){
                                    return false
                                }
                            }
                        }else{
                            return $.validForm.validate($elem,_value,_validates);
                        }
                    }

                }
            }
        });
    };

    $.validForm = {
        validate:function ($elem,value,valid) {
            var _validType = valid["type"],
                _validParam = valid["value"],
                _message = valid["message"];
            var r = true;
            if(_validParam && _validParam.length){
                r = validator[_validType](value,_validParam)
            }else{
                r = validator[_validType](value)
            }
            this.setValidCss($elem,r,_message);
            return r;
        },
        setValidCss:function ($elem,result,message) {
            if(result){
                if($elem.hasClass("valid-error")){
                    $elem.next("span").remove();
                    $elem.removeClass("valid-error");
                }
                $elem.addClass("valid-success");
            }else{
                if(!$elem.hasClass("valid-error")){
                    $elem.removeClass("valid-success");
                    $elem.addClass("valid-error");
                    $elem.after($(_validSpanElem));
                    $elem.next("span").find("span").text(message);
                }else{
                    $elem.next("span").fadeIn();
                }
                setTimeout(function () {
                    $elem.next("span").fadeOut(300);
                },3000)
            }


        }
    }
})(jQuery,window,undefined);
